package com.dmd.yikang.controller;

/**
 * @author Daimingdong
 * @version 1.0.0
 * @title MedicineController
 * @create 2024/5/30 10:21
 * @description
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmd.yikang.common.R;
import com.dmd.yikang.dto.MedicineDto;
import com.dmd.yikang.entity.Category;
import com.dmd.yikang.entity.Medicine;
import com.dmd.yikang.entity.MedicineType;
import com.dmd.yikang.service.CategoryService;
import com.dmd.yikang.service.MedicineTypeService;
import com.dmd.yikang.service.MedicineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 药品管理
 */

@RestController
@RequestMapping("/medicine")
@Slf4j
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    @Autowired
    private MedicineTypeService medicineTypeService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增药品
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody MedicineDto medicineDto){
        log.info(medicineDto.toString());

        medicineService.saveWithType(medicineDto);
        return R.success("新增药品成功");
    }

    /**
     * 药品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        // 构造分页构造器
        Page<Medicine> pageInfo = new Page<>(page,pageSize);
        Page<MedicineDto> medicineDtoPage = new Page<>();

        // 构造查询条件
        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(name != null,Medicine::getName,name);
        // 添加排序条件
        queryWrapper.orderByDesc(Medicine::getUpdateTime);
        // 执行查询
        medicineService.page(pageInfo,queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,medicineDtoPage,"records");

        List<Medicine> records = pageInfo.getRecords();

        List<MedicineDto> list = records.stream().map(item -> {
            MedicineDto medicineDto = new MedicineDto();

            BeanUtils.copyProperties(item,medicineDto);

            Long categoryId = item.getCategoryId();

            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                medicineDto.setCategoryName(categoryName);
            }

            String categoryName = category.getName();
            medicineDto.setCategoryName(categoryName);

            return medicineDto;
        }).collect(Collectors.toList());

        medicineDtoPage.setRecords(list);

        return R.success(medicineDtoPage);
    }

    /**
     * 根据id查询药品信息和药品类型信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<MedicineDto> get(@PathVariable Long id){

        MedicineDto medicineDto = medicineService.getByIdWithType(id);

        return R.success(medicineDto);
    }

    /**
     * 修改药品信息
     * @param medicineDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody MedicineDto medicineDto){
        log.info(medicineDto.toString());

        medicineService.updateWithType(medicineDto);

        return R.success("修改成功");
    }

    /**
     * 对药品进行批量或是单个的停售和启售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){

        LambdaQueryWrapper<Medicine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null,Medicine::getId,ids);

        //根据数据进行批量查询
        List<Medicine> list = medicineService.list(queryWrapper);

        for (Medicine medicine : list){
            if (medicine != null){
                medicine.setStatus(status);
                medicineService.updateById(medicine);
            }
        }
        return R.success("售卖状态修改成功");
    }

    /**
     * 对药品进行批量或是单个的删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        //删除药品，逻辑删除
        medicineService.deleteById(ids);
        //删除药品对应的类型，逻辑删除
        LambdaQueryWrapper<MedicineType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MedicineType::getMedicineId,ids);
        medicineTypeService.remove(queryWrapper);

        return R.success("药品删除成功");
    }
}
